<?php
/**
 * 设置管理
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
class SettingController extends CommonController{	
	private $msg=array();
	private $Model;
	private $type=array();
	function _initialize(){
		parent::_initialize();
		$this->Model=M('Setting');
		$this->type=array(L('text_type_input'),L('text_type_textarea'),L('text_type_image'),L('text_type_editor'));
	}
	public function index(){
		if(!$this->checkPermission('Setting')) $this->jump;
		$count=$this->Model->count();
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$show=$Page->show();
		$settings=$this->Model->order('sort_order ASC')->limit($Page->firstRow,$Page->listRows)->select();
		$SettingGroupModel=M('SettingGroup');
		$language_id=get_default_lng_id();
		$list=array();
		foreach ($settings as $k  => $setting) {
			$text=json_decode($setting['text'],true);
			$group=$SettingGroupModel->where(array('setting_group_id'=>$setting['setting_group_id']))->find();
			$group['text']=json_decode($group['text'],true);
			$list[]=array(
				'setting_id'	=>	$setting['setting_id'],
				'title'			=>	$text[$language_id]['title'],
				'key'			=>	$setting['key'],
				'group_title'	=>	$group['text'][$language_id]['title'],
				'group_link'	=>	U('SettingGroup/oper?setting_group_id='.$group['setting_group_id']),
				'type'			=>	$this->type[$setting['type']],
				'sort_order'	=>	$setting['sort_order']
				);
		}
		$url['p']=I('get.p','');
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('url',$url);
		$this->display();
	}

	public function oper($setting_id=0){
		if(!$setting_id && !$this->checkPermission('AddSetting')) $this->jump();
		$map=array('setting_id'=>$setting_id);
		if($setting_id) $setting=$this->Model->where($map)->find();
		if($setting_id && empty($setting)) $this->jump(L('error_404'));
		if($setting_id) $setting['text']=json_decode($setting['text'],true);
		if(IS_POST){
			$setting=I('post.');
			if($this->validateForm()){
				$setting['text']=json_encode($setting['text']);
				if($setting_id){
					$this->Model->where($map)->save($setting);
				}else{
					$this->Model->add($setting);
				}
				$url['p']=I('get.p','');
				session('success',$setting_id?L('success_edit'):L('success_add'));
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$setting_group=M('SettingGroup')->order('sort_order asc')->select();
		$sg=array();
		$language_id=get_default_lng_id();
		foreach ($setting_group as $k => $v) {
				$text=json_decode($v['text'],true);
			$sg[]=array(			
				'setting_group_id'	=>	$v['setting_group_id'],
				'title'				=>	$text[$language_id]['title']
				);
		}
		$this->assign('setting_group',$sg);
		$this->assign('setting',$setting);
		$this->assign('msg',$this->msg);
		$this->assign('type',$this->type);
		$this->assign('languages',get_langs());
		$this->display();
	}
	public function del(){
		if($this->checkPermission('DeleteSetting')){
			$ids=I('post.selected');
			$map['setting_id']=array('IN',$ids);
			$count=0;
			$count=$this->Model->where($map)->delete();
			session('success',L('success_delete',array('count'=>$count)));
		}else{
			session('danger',L('error_not_permission'));
		}
		$url['p']=I('get.p');
		$this->redirect('index',$url);
	}

	public function setting(){
		if(!$this->checkPermission('SettingSetting')) $this->jump();
		if(IS_POST){
			$setting=I('post.');
			foreach ($setting as $key => $value) {
				$map=array('key'=>$key);
				$this->Model->where($map)->setField('value',json_encode($value));
			}
			session('success',L('success_edit'));
			$this->redirect('setting');
		}
		unset($key);unset($value);unset($map);
		$sg=M('SettingGroup')->order('sort_order asc')->select();
		$setting_group=array();
		$language_id=get_default_lng_id();
		$placeholder=image_resize();
		$languages=get_langs();
		foreach ($sg as $k => $v) {
			$group_text=json_decode($v['text'],true);
			$map=array('setting_group_id'=>$v['setting_group_id']);
			$settings=$this->Model->where($map)->order('sort_order asc')->select();
			$s=array();
			foreach ($settings as $key => $value) {
				$text=json_decode($value['text'],true);
				$setting_value=json_decode($value['value'],true);
				if($value['type']==2){
					$thumb=array();
					foreach($languages as $language){
						if(isset($setting_value[$language['language_id']]) && is_file(C('IMAGE_DIR').$setting_value[$language['language_id']])){
							$thumb[$language['language_id']]=image_resize($setting_value[$language['language_id']],100,100);
						}else{
							$thumb[$language['language_id']]=$placeholder;
						}
					}
				}
				$s[]=array(
					'key'	=>	$value['key'],
					'title'	=>	$text[$language_id]['title'],
					'type'	=>	$value['type'],
					'value'	=>	$setting_value,
					'thumb'	=>	$thumb
					);	
			}
			if($s){
				$setting_group[]=array(
					'title'		=>	$group_text[$language_id]['title'],
					'setting_group_id'=>	$v['setting_group_id'],
					'setting'	=>	$s	
					);
			}

		}
		$this->assign('setting_group',$setting_group);
		$this->assign('languages',$languages);
		$this->assign('placeholder',$placeholder);
		$this->display();
	}
	private function validateForm(){
		$setting_id=I('get.setting_id',0);
		if($setting_id && !$this->checkPermission('EditSetting')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach ($data['text'] as $language_id => $v) {

			if(mb_strlen($v['title'])<2 || mb_strlen($v['title'])>50) $this->msg['title'][$language_id]=L('error_title');
		}
		if($setting_id){
			$map=array(
				'setting_id'	=>array('neq',$setting_id),
				'key'			=>$data['key']
				);
			if($this->Model->where($map)->find()) $this->msg['key']=L('error_key_exist');
		}
		return !$this->msg;
	}
}